package com.zxl.grocery.modular.crawler.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zxl.grocery.core.base.controller.BaseController;
import com.zxl.grocery.core.log.LogObjectHolder;
import com.zxl.grocery.core.reqres.response.ResponseData;
import com.zxl.grocery.core.shiro.ShiroKit;
import com.zxl.grocery.core.shiro.ShiroUser;
import com.zxl.grocery.modular.crawler.entity.CrawlerTarget;
import com.zxl.grocery.modular.crawler.model.CrawlerTargetDto;
import com.zxl.grocery.modular.crawler.service.CrawlerTargetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * CrawlerTarget控制器
 *
 * @author zxl
 * @Date 2018-08-02 17:46:19
 */
@Controller
@RequestMapping("/crawlerTarget")
public class CrawlerTargetController extends BaseController {

    private static String PREFIX = "/crawler/crawlerTarget/";

    @Autowired
    private CrawlerTargetService crawlerTargetService;

    /**
     * 跳转到CrawlerTarget首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "crawlerTarget.html";
    }

    /**
     * 跳转到添加CrawlerTarget
     */
    @RequestMapping("/crawlerTarget_add")
    public String crawlerTargetAdd() {
        return PREFIX + "crawlerTarget_add.html";
    }

    /**
     * 跳转到修改CrawlerTarget
     */
    @RequestMapping("/crawlerTarget_update")
    public String crawlerTargetUpdate(@RequestParam Integer id) {
        CrawlerTarget crawlerTarget = crawlerTargetService.getById(id);
//        LogObjectHolder.me().set(crawlerTarget);
        return PREFIX + "crawlerTarget_edit.html";
    }

    /**
     * 获取CrawlerTarget列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "name", required = false) String name) {
        System.out.println("name:" + name);
        return crawlerTargetService.getById(null);
    }

    /**
     * 新增CrawlerTarget
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CrawlerTarget crawlerTarget) {
        ShiroUser shiroUser = ShiroKit.getUserNotNull();
        crawlerTarget.setCreateUserId(shiroUser.getId());
        crawlerTarget.setCreateDate(new Date());
        crawlerTargetService.save(crawlerTarget);
        return SUCCESS_TIP;
    }

    /**
     * 删除CrawlerTarget
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer crawlerTargetId) {
        crawlerTargetService.removeById(crawlerTargetId);
        return SUCCESS_TIP;
    }


    /**
     * 修改CrawlerTarget
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CrawlerTargetDto crawlerTargetDto) {
        ShiroUser shiroUser = ShiroKit.getUserNotNull();
        CrawlerTarget crawlerTarget = new CrawlerTarget();
        BeanUtil.copyProperties(crawlerTargetDto, crawlerTarget);
        crawlerTarget.setUpdateUserId(shiroUser.getId());
        crawlerTarget.setModifyDate(new Date());
        crawlerTargetService.updateById(crawlerTarget);
        return SUCCESS_TIP;
    }

    /**
     * CrawlerTarget详情
     */
    @RequestMapping(value = "/detail/{crawlerTargetId}")
    @ResponseBody
    public ResponseData detail(@PathVariable("crawlerTargetId") Integer crawlerTargetId) {
        CrawlerTarget crawlerTarget = crawlerTargetService.getById(crawlerTargetId);
        LogObjectHolder.me().set(crawlerTarget);
        Map<String, Object> crawlerTargetMap = BeanUtil.beanToMap(crawlerTarget);
        return ResponseData.success(crawlerTargetMap);
    }
}
